home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / Dylan Related / Mindy / Mindy 1.2 - portable sources / comp / free.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  11.9 KB  |  551 lines  |  [TEXT/ttxt]

  1. /**********************************************************************\
  2. *
  3. *  Copyright (c) 1994  Carnegie Mellon University
  4. *  All rights reserved.
  5. *  
  6. *  Use and copying of this software and preparation of derivative
  7. *  works based on this software are permitted, including commercial
  8. *  use, provided that the following conditions are observed:
  9. *  
  10. *  1. This copyright notice must be retained in full on any copies
  11. *     and on appropriate parts of any derivative works.
  12. *  2. Documentation (paper or online) accompanying any system that
  13. *     incorporates this software, or any part of it, must acknowledge
  14. *     the contribution of the Gwydion Project at Carnegie Mellon
  15. *     University.
  16. *  
  17. *  This software is made available "as is".  Neither the authors nor
  18. *  Carnegie Mellon University make any warranty about the software,
  19. *  its performance, or its conformity to any specification.
  20. *  
  21. *  Bug reports, questions, comments, and suggestions should be sent by
  22. *  E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  23. *
  24. ***********************************************************************
  25. *
  26. * $Header: free.c,v 1.5 94/10/05 20:54:56 nkramer Exp $
  27. *
  28. * This file frees parts of the parse tree.
  29. *
  30. \**********************************************************************/
  31.  
  32. #include "../compat/std-c.h"
  33.  
  34. #include "mindycomp.h"
  35. #include "src.h"
  36. #include "literal.h"
  37. #include "free.h"
  38. #include "lose.h"
  39.  
  40.  
  41. /* Utilities. */
  42.  
  43. static void free_id(struct id *id)
  44. {
  45.     free(id);
  46. }
  47.  
  48. static void free_params(struct param_list *params)
  49. {
  50.     struct param *param, *next_param;
  51.     struct keyword_param *key, *next_key;
  52.  
  53.     for (param = params->required_params; param != NULL; param = next_param) {
  54.     free_id(param->id);
  55.     if (param->type)
  56.         free_expr(param->type);
  57.     next_param = param->next;
  58.     free(param);
  59.     }
  60.     if (params->next_param)
  61.     free_id(params->next_param);
  62.     if (params->rest_param)
  63.     free_id(params->rest_param);
  64.     for (key = params->keyword_params; key != NULL; key = next_key) {
  65.     free_id(key->id);
  66.     if (key->type)
  67.         free_expr(key->type);
  68.     if (key->def)
  69.         free_expr(key->def);
  70.     next_key = key->next;
  71.     free(key);
  72.     }
  73.     free(params);
  74. }
  75.  
  76. static void free_bindings(struct bindings *bindings)
  77. {
  78.     free_params(bindings->params);
  79.     if (bindings->expr)
  80.     free_expr(bindings->expr);
  81.     free(bindings);
  82. }
  83.  
  84. static void free_rettypes(struct return_type_list *rettypes)
  85. {
  86.     struct return_type *r, *next;
  87.  
  88.     for (r = rettypes->req_types; r != NULL; r = next) {
  89.     if (r->type)
  90.         free_expr(r->type);
  91.     next = r->next;
  92.     free(r);
  93.     }
  94.     if (rettypes->req_types_list)
  95.     free_expr(rettypes->req_types_list);
  96.     if (rettypes->rest_type)
  97.     free_expr(rettypes->rest_type);
  98.     if (rettypes->rest_temp_varref)
  99.     free_expr(rettypes->rest_temp_varref);
  100.     free(rettypes);
  101. }
  102.  
  103. static void free_plist(struct plist *plist)
  104. {
  105.     struct property *prop, *next;
  106.  
  107.     for (prop = plist->head; prop != NULL; prop = next) {
  108.     free_expr(prop->expr);
  109.     next = prop->next;
  110.     free(prop);
  111.     }
  112.     free(plist);
  113. }
  114.  
  115. static void free_condition_body(struct condition_body *body)
  116. {
  117.     struct condition_body *next;
  118.     struct condition_clause *clause;
  119.     struct condition *cond, *next_cond;
  120.  
  121.     while (body != NULL) {
  122.     clause = body->clause;
  123.     for (cond = clause->conditions; cond != NULL; cond = next_cond) {
  124.         free_expr(cond->cond);
  125.         next_cond = cond->next;
  126.         free(cond);
  127.     }
  128.     free_body(clause->body);
  129.     free(clause);
  130.     next = body->next;
  131.     free(body);
  132.     body = next;
  133.     }
  134. }
  135.  
  136. static void free_method(struct method *method)
  137. {
  138.     if (method->name)
  139.     free_id(method->name);
  140.     if (method->debug_name)
  141.     free_literal(method->debug_name);
  142.     free_params(method->params);
  143.     if (method->specializers)
  144.     free_expr(method->specializers);
  145.     if (method->rettypes)
  146.     free_rettypes(method->rettypes);
  147.     free_body(method->body);
  148.     /* ### Free the lexenv? */
  149.     free(method);
  150. }
  151.  
  152.  
  153.  
  154. /* Stuff to free expressions. */
  155.  
  156. static void free_varref_expr(struct varref_expr *e)
  157. {
  158.     free_id(e->var);
  159.     free(e);
  160. }
  161.  
  162. static void free_literal_expr(struct literal_expr *e)
  163. {
  164.     free_literal(e->lit);
  165.     free(e);
  166. }
  167.  
  168. static void free_call_expr(struct call_expr *e)
  169. {
  170.     struct argument *arg, *next;
  171.  
  172.     free_expr(e->func);
  173.     for (arg = e->args; arg != NULL; arg = next) {
  174.     next = arg->next;
  175.     free_expr(arg->expr);
  176.     free(arg);
  177.     }
  178.     free(e);
  179. }
  180.  
  181. static void free_method_expr(struct method_expr *e)
  182. {
  183.     free_method(e->method);
  184.     free(e);
  185. }
  186.  
  187. static void free_dot_expr(struct dot_expr *e)
  188. {
  189.     free_expr(e->arg);
  190.     free_expr(e->func);
  191.     free(e);
  192. }
  193.  
  194. static void free_body_expr(struct body_expr *e)
  195. {
  196.     free_body(e->body);
  197.     free(e);
  198. }
  199.  
  200. static void free_exception_clauses(struct exception_clause *clauses)
  201. {
  202.     struct exception_clause *clause, *next;
  203.  
  204.     for (clause = clauses; clause != NULL; clause = next) {
  205.     free_expr(clause->type);
  206.     if (clause->condition)
  207.         free_id(clause->condition);
  208.     free_plist(clause->plist);
  209.     free_body(clause->body);
  210.     next = clause->next;
  211.     free(clause);
  212.     }
  213. }
  214.  
  215. static void free_block_expr(struct block_expr *e)
  216. {
  217.     if (e->exit_fun)
  218.     free_id(e->exit_fun);
  219.     free_body(e->body);
  220.     free_exception_clauses(e->inner);
  221.     if (e->cleanup)
  222.     free_body(e->cleanup);
  223.     free_exception_clauses(e->outer);
  224.     free(e);
  225. }
  226.  
  227. static void free_case_expr(struct case_expr *e)
  228. {
  229.     free_condition_body(e->body);
  230.     free(e);
  231. }
  232.  
  233. static void free_if_expr(struct if_expr *e)
  234. {
  235.     free_expr(e->cond);
  236.     free_body(e->consequent);
  237.     free_body(e->alternate);
  238.     free(e);
  239. }
  240.  
  241. static void free_for_expr(struct for_expr *e)
  242. {
  243.     struct for_clause *clause, *next;
  244.  
  245.     for (clause = e->clauses; clause != NULL; clause = next) {
  246.     free_params(clause->vars);
  247.     switch (clause->kind) {
  248.       case for_EQUAL_THEN:
  249.         {
  250.         struct equal_then_for_clause *c
  251.             = (struct equal_then_for_clause *)clause;
  252.         free_expr(c->equal);
  253.         free_expr(c->then);
  254.         break;
  255.         }
  256.       case for_IN:
  257.         {
  258.         struct in_for_clause *c = (struct in_for_clause *)clause;
  259.         free_expr(c->collection);
  260.         break;
  261.         }
  262.       case for_FROM:
  263.         {
  264.         struct from_for_clause *c = (struct from_for_clause *)clause;
  265.         free_expr(c->from);
  266.         if (c->to)
  267.             free_expr(c->to);
  268.         if (c->by)
  269.             free_expr(c->by);
  270.         }
  271.       default:
  272.         lose("Bogus for clause kind.");
  273.     }
  274.     next = clause->next;
  275.     free(clause);
  276.     }
  277.     if (e->until)
  278.     free_expr(e->until);
  279.     free_body(e->body);
  280.     if (e->finally)
  281.     free_body(e->finally);
  282.     free(e);
  283. }
  284.  
  285. static void free_select_expr(struct select_expr *e)
  286. {
  287.     free_expr(e->expr);
  288.     if (e->by)
  289.     free_expr(e->by);
  290.     free_condition_body(e->body);
  291.     free(e);
  292. }
  293.  
  294. static void free_varset_expr(struct varset_expr *e)
  295. {
  296.     free_id(e->var);
  297.     free_expr(e->value);
  298.     free(e);
  299. }
  300.  
  301. static void free_binop_series_expr(struct binop_series_expr *e)
  302. {
  303.     struct binop *b, *next;
  304.  
  305.     free_expr(e->first_operand);
  306.     for (b = e->first_binop; b != NULL; b = next) {
  307.     free_id(b->op);
  308.     free_expr(b->operand);
  309.     next = b->next;
  310.     free(b);
  311.     }
  312.     free(e);
  313. }
  314.  
  315. static void free_loop_expr(struct loop_expr *e)
  316. {
  317.     free_body(e->body);
  318.     free(e);
  319. }
  320.  
  321. static void free_repeat_expr(struct repeat_expr *e)
  322. {
  323.     free(e);
  324. }
  325.  
  326. static void free_error_expr(struct expr *e)
  327. {
  328.     free(e);
  329. }
  330.  
  331. static void (*ExprFreers[(int)expr_Kinds])() = {
  332.     free_varref_expr, free_literal_expr, free_call_expr,
  333.     free_method_expr, free_dot_expr, free_body_expr, free_block_expr,
  334.     free_case_expr, free_if_expr, free_for_expr, free_select_expr,
  335.     free_varset_expr, free_binop_series_expr, free_loop_expr,
  336.     free_repeat_expr, free_error_expr
  337. };
  338.  
  339. void free_expr(struct expr *e)
  340. {
  341.     (*ExprFreers[(int)e->kind])(e);
  342. }
  343.  
  344.  
  345. /* Stuff to free constituents. */
  346.  
  347. static void free_defconst_constituent(struct defconst_constituent *c)
  348. {
  349.     free_bindings(c->bindings);
  350.     if (c->tlf)
  351.     free_method(c->tlf);
  352.     free(c);
  353. }
  354.  
  355. static void free_defvar_constituent(struct defvar_constituent *c)
  356. {
  357.     free_bindings(c->bindings);
  358.     if (c->tlf)
  359.     free_method(c->tlf);
  360.     free(c);
  361. }
  362.  
  363. static void free_defmethod_constituent(struct defmethod_constituent *c)
  364. {
  365.     if (c->tlf)
  366.     free_method(c->tlf);
  367.     else
  368.     free_method(c->method);
  369.     free(c);
  370. }
  371.  
  372. static void free_defgeneric_constituent(struct defgeneric_constituent *c)
  373. {
  374.     free_id(c->name);
  375.     if (c->params)
  376.     free_params(c->params);
  377.     if (c->rettypes)
  378.     free_rettypes(c->rettypes);
  379.     if (c->plist)
  380.     free_plist(c->plist);
  381.     if (c->tlf)
  382.     free_method(c->tlf);
  383. }
  384.  
  385. static void free_defclass_constituent(struct defclass_constituent *c)
  386. {
  387.     /* ### free_defclass_constituent not written yet. */
  388. }
  389.  
  390. static void free_expr_constituent(struct expr_constituent *c)
  391. {
  392.     free_expr(c->expr);
  393.     free(c);
  394. }
  395.  
  396. static void free_local_constituent(struct local_constituent *c)
  397. {
  398.     struct method *method, *next;
  399.  
  400.     free_body(c->body);
  401.     for (method = c->methods; method != NULL; method = next) {
  402.     next = method->next_local;
  403.     free_method(method);
  404.     }
  405.     /* ### free the lexenv? */
  406.     free(c);
  407. }
  408.  
  409. static void free_handler_constituent(struct handler_constituent *c)
  410. {
  411.     free_body(c->body);
  412.     if (c->type)
  413.     free_expr(c->type);
  414.     if (c->func)
  415.     free_expr(c->func);
  416.     if (c->plist)
  417.     free_plist(c->plist);
  418.     free(c);
  419. }
  420.  
  421. static void free_let_constituent(struct let_constituent *c)
  422. {
  423.     free_body(c->body);
  424.     free_bindings(c->bindings);
  425.     /* ### Free c->lexenv? */
  426.     free(c);
  427. }
  428.  
  429. static void free_tlf_constituent(struct tlf_constituent *c)
  430. {
  431.     free_method(c->form);
  432.     free(c);
  433. }
  434.  
  435. static void free_error_constituent(struct constituent *c)
  436. {
  437.     free(c);
  438. }
  439.  
  440. static void free_var_names(struct variable_names *names)
  441. {
  442.     struct variable_name *v, *next;
  443.  
  444.     for (v = names->head; v != NULL; v = next) {
  445.     
  446.     next = v->next;
  447.     free(v);
  448.     }
  449.     free(names);
  450. }
  451.  
  452. static void free_renamings(struct renamings *renamings)
  453. {
  454.     struct renaming *ren, *next;
  455.  
  456.     for (ren = renamings->head; ren != NULL; ren = next) {
  457.     free_literal(ren->from);
  458.     free_literal(ren->to);
  459.     next = ren->next;
  460.     free(ren);
  461.     }
  462.     free(renamings);
  463. }
  464.  
  465. static void free_defnamespace_constituent(struct defnamespace_constituent *c)
  466. {
  467.     struct use_clause *use, *next;
  468.  
  469.     free_literal(c->name);
  470.  
  471.     for (use = c->use_clauses; use != NULL; use = next) {
  472.     struct use_option *opt, *next_opt;
  473.  
  474.     for (opt = use->options; opt != NULL; opt = next_opt) {
  475.         switch (opt->kind) {
  476.           case useopt_PREFIX:
  477.         free_literal(((struct prefix_option *)opt)->prefix);
  478.         break;
  479.           case useopt_IMPORT:
  480.         free_var_names(((struct import_option *)opt)->vars);
  481.         free_renamings(((struct import_option *)opt)->renames);
  482.         break;
  483.           case useopt_EXCLUDE:
  484.         free_var_names(((struct exclude_option *)opt)->vars);
  485.         break;
  486.           case useopt_RENAME:
  487.         free_renamings(((struct rename_option *)opt)->renames);
  488.         break;
  489.           case useopt_EXPORT:
  490.         free_var_names(((struct export_option *)opt)->vars);
  491.         break;
  492.           case useopt_IMPORT_ALL:
  493.           case useopt_EXPORT_ALL:
  494.         break;
  495.         }
  496.         next_opt = opt->next;
  497.         free(opt);
  498.     }
  499.  
  500.     if (use->import)
  501.         free_literal(use->import);
  502.     if (use->exclude)
  503.         free_literal(use->exclude);
  504.     if (use->prefix)
  505.         free_literal(use->prefix);
  506.     if (use->rename)
  507.         free_literal(use->rename);
  508.     if (use->export)
  509.         free_literal(use->export);
  510.  
  511.     next = use->next;
  512.  
  513.     free(use);
  514.     }
  515.  
  516.     if (c->exported_variables)
  517.     free_var_names(c->exported_variables);
  518.     if (c->created_variables)
  519.     free_var_names(c->created_variables);
  520.  
  521.     if (c->exported_literal)
  522.     free_literal(c->exported_literal);
  523.     if (c->created_literal)
  524.     free_literal(c->created_literal);
  525. }
  526.  
  527. static void (*ConstituentFreers[(int)constituent_Kinds])() = {
  528.     free_defconst_constituent, free_defvar_constituent,
  529.     free_defmethod_constituent, free_defgeneric_constituent,
  530.     free_defclass_constituent, free_expr_constituent,
  531.     free_local_constituent, free_handler_constituent,
  532.     free_let_constituent, free_tlf_constituent, free_error_constituent,
  533.     free_defnamespace_constituent, free_defnamespace_constituent
  534. };
  535.  
  536. void free_constituent(struct constituent *c)
  537. {
  538.     (*ConstituentFreers[(int)c->kind])(c);
  539. }
  540.  
  541. void free_body(struct body *body)
  542. {
  543.     struct constituent *ptr, *next;
  544.  
  545.     for (ptr = body->head; ptr != NULL; ptr = next) {
  546.     next = ptr->next;
  547.     free_constituent(ptr);
  548.     }
  549.     free(body);
  550. }
  551.